﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EntityFrameworks.Application.Core.OnlinePay
{
    /// <summary>
    /// 在线支付调用的实现
    /// </summary>
    public class Demo
    {
        /// <summary>
        /// 充值ＰＯＳＴ
        /// </summary>
        public void UserRecharge()
        {
            decimal price = 1;
            int userID = 1;
            var alipayImpl = new AlipayImpl();
            alipayImpl.RechargeTo(price, userID);
        }
        /// <summary>
        /// 订单ＰＯＳＴ
        /// </summary>
        public void UserOrder()
        {
            int userID = 0, sourceType = 0;
            decimal price = 1;
            string orderID = "OR001", name = "zzl";
            var alipayImpl = new AlipayImpl();
            alipayImpl.OnlineTo(orderID, sourceType, userID, price, name);
        }

        #region 第三方回调与本地事件订阅相关
        /// <summary>
        /// 支付宝返回页面
        /// </summary>
        public void AlipayReturn()
        {
            var alipayImpl = new AlipayImpl();
            alipayImpl.Success += alipayImpl_Success;
            alipayImpl.Fail += alipayImpl_Fail;
            alipayImpl.Notify_Return(HttpMethod.Get);
            alipayImpl.Success -= alipayImpl_Success;
            alipayImpl.Fail -= alipayImpl_Fail;

        }
        /// <summary>
        /// 支付宝通知页面
        /// </summary>
        public void AlipayNotify()
        {
            var alipayImpl = new AlipayImpl();
            alipayImpl.Success += alipayImpl_Success;
            alipayImpl.Fail += alipayImpl_Fail;
            alipayImpl.Notify_Return(HttpMethod.Post);
            alipayImpl.Success -= alipayImpl_Success;
            alipayImpl.Fail -= alipayImpl_Fail;
        }
        /// <summary>
        /// 第三方支付如果成功，就解锁余额
        /// </summary>
        /// <param name="e"></param>
        void alipayImpl_Success(OrderEventArgs e)
        {
            if (e.UseType == UseType.Order)
            {
                //订单交易成功
            }
            else
            {
                //充值交易成功
            }
        }

        /// <summary>
        /// 第三方返回失败的逻辑
        /// </summary>
        /// <param name="e"></param>
        void alipayImpl_Fail(OrderEventArgs e)
        {
            if (e.UseType == UseType.Order)
            {
                //订单交易失败
            }
            else
            {
                //充值交易失败
            }
        }
        #endregion
    }
}
